package client;

import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;

import pojo.Protocol;

public class SimpleTest extends JFrame implements ActionListener{
	private static final long serialVersionUID = 1L;
	
	private TextArea ta1;
	private TextArea ta2;
	private TextArea ta3;
	private TextArea ta4;
	private TextArea ta5;
	private TextArea ta6;
	private JButton button;
	private Socket socket;
	
	public SimpleTest() throws IOException{
		this.setSize(400,650);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("QQ cheap copy");
		
		init();
		addEvent();
		this.setVisible(true);
	}
	
	private void init() throws UnknownHostException, IOException{
		this.setLayout(null);
		ta1 = new TextArea();
		ta2 = new TextArea();
		ta3 = new TextArea();
		ta4 = new TextArea();
		ta5 = new TextArea();
		ta6 = new TextArea();
		button = new JButton("send");
		
		ta1.setBounds(0, 0, 400, 50);
		ta2.setBounds(0, 50, 400, 50);
		ta3.setBounds(0, 100, 400, 50);
		ta4.setBounds(0, 150, 400, 50);
		ta5.setBounds(0, 200, 400, 50);
		ta6.setBounds(0, 250, 400, 50);
		button.setBounds(0, 400, 100, 50);
		
		this.add(ta1);
		this.add(ta2);
		this.add(ta3);
		this.add(ta4);
		this.add(ta5);
		this.add(ta6);
		this.add(button);
		
		socket = new Socket("10.222.29.181", 8081);
		
	}
	private void addEvent(){
		button.addActionListener(this);
	}
	
	
	public static void main(String[] args) throws IOException {
		new SimpleTest();
	}

	public void actionPerformed(ActionEvent e) {
		int type = Integer.parseInt(ta1.getText());
		String msg = ta2.getText();
		String from = ta3.getText();
		String to = ta4.getText();
		List<String> set = null;
		Date time = Calendar.getInstance().getTime(); 
		
		Protocol protocol = new Protocol(type, msg, from, to, null);
		System.out.println(protocol.toString());
		
		try {
			
			ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
			
			out.writeObject(protocol);
			System.out.println("writed");
			
			ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
			
			Protocol receive = (Protocol)in.readObject();
			System.out.println("geted!");
			
			type = receive.getType();
			msg = receive.getMsg();
			from = receive.getFrom();
			to = receive.getTo();
			set = receive.getOnlineList();
			time = receive.getTime();
			
			System.out.println(type+"\n"+msg+"\n"+from+"\n"+to+"\n"+set.toString()+"\n"+time);
			
			
			
			
		} catch (UnknownHostException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (ClassNotFoundException e3) {
			// TODO Auto-generated catch block
			e3.printStackTrace();
		}
		
	}

}
